#!/bin/sh
#
#  This shell script gives you some examples how to use the bc library.
#

# Here we include libinclude.sh
. libinclude.sh

# Here we import the bc library which comes with two functions:
# cls : do calculation/validation
# e2d : convert from exponential to floating point format
import bc

echo 
# Example 1: A simple calculation.
echo "Example 1:"
echo -n "(45.88*2^4-1.4)/1.345 = "
bc_clc "(45.88*2^4-1)/1.345"
# now we reset the SCALE variable...
bc_SCALE=6
echo -n "with bc_SCALE=$bc_SCALE: "
# and repeat the calculation
bc_clc "(45.88*2^4-1)/1.345"

echo
# Example 2: Do a calculation with variables.
echo 'Example 2:'
echo -n "a = "
read a
echo -n "b = "
read b
c=$(bc_clc "($a/$b)")
echo "-> $a/$b = $c"

echo
# Example 3: Evaluate a statement.
echo 'Example 3 Evaluate a statement:'
echo -n "$a < $b : "
if [ $(bc_clc "$a < $b") = 1 ]; then
	echo 'TRUE'
else
      echo 'FALSE'
fi
echo
# Example 4: Convert from exp to float.
# Because bc can't handle exponential format, it can be necessary to convert
# input variables before.
echo 'Example 4 Convert from exp to float:'
VAL='3.7865e-4'
CVAL=$(bc_e2d $VAL)
echo "$VAL -> $CVAL"

